package com.mnsq.common.tools.valid;


import com.mnsq.common.tools.tool.TimeUtils;

import java.util.Date;

public class SignUtils {
    private static final Integer SING_INTEGER = 2000;

    /**
     * 验证时间签名
     *
     * @param str
     * @return
     */
    public static boolean checkSign(String str) {
        if (str.length() < 13 || str.length() > 15) {
            return false;
        }
        String actionTime = str.substring(0, 10);
        String reqSign = str.substring(10);
        Long s = Long.valueOf(actionTime) & SING_INTEGER;
        if (reqSign.equals(s.toString())) {
            Date date = new Date(Long.valueOf(actionTime) * 1000);
            Long second = TimeUtils.getSubtractDateMillis(date, new Date());
            if (second / 60 < 3) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取时间签名
     *
     * @return
     */
    public static String getSign() {
        Long time = TimeUtils.getCurrentTime() / 1000;
        Long suffix = time & SING_INTEGER;
        return time.toString() + suffix;
    }
}
